#include "weldsystemdialog.h"
#include "ui_weldsystemdialog.h"
#include <QDebug>

WeldSystemDialog::WeldSystemDialog(int styleId,CommunicateNormalDelegate *communicateNormalIn,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WeldSystemDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
    communicateNormalDelegate = communicateNormalIn;
}

void WeldSystemDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->scrollArea->setGeometry(10,40,460,400);
        ui->returnBtn->setGeometry(500,400,70,40);
        ui->saveBtn->setGeometry(500,350,70,40);
        break;
    default:
        break;
    }
}

WeldSystemDialog::~WeldSystemDialog()
{
    delete ui;
}

void WeldSystemDialog::showView_slot(){
    this->show();
    //获取弧焊系统信息
    communicateNormalDelegate->getWeldSystemInfo(weldSystemInfo);

    if(weldSystemInfo.arcDepletionDetection ==1){
        ui->checkBox->setChecked(true);
    }else {
        ui->checkBox->setChecked(false);
    }

    if(weldSystemInfo.gasDepletionDetection ==1){
        ui->checkBox_2->setChecked(true);
    }else {
        ui->checkBox_2->setChecked(false);
    }

    if(weldSystemInfo.weldwireDepletionDetection ==1){
        ui->checkBox_3->setChecked(true);
    }else {
        ui->checkBox_3->setChecked(false);
    }

    if(weldSystemInfo.solubleApplyTesting ==1){
        ui->checkBox_4->setChecked(true);
    }else{
        ui->checkBox_4->setChecked(false);
    }

    if(weldSystemInfo.powerFailureDetection ==1){
       ui->checkBox_5->setChecked(true);
    }else {
       ui->checkBox_5->setChecked(false);
    }

    if(weldSystemInfo.weldwireDepletionDetection ==1){
       ui->checkBox_6->setChecked(true);
    }else {
       ui->checkBox_6->setChecked(false);
    }

    if(weldSystemInfo.toOpenAction ==1){
        ui->checkBox_7->setChecked(true);
    }else {
        ui->checkBox_7->setChecked(false);
    }

    ui->lineEdit->setText(QString("%1").arg(weldSystemInfo.toOpenDistance));
    ui->lineEdit_2->setText(QString("%1").arg(weldSystemInfo.toOpenSpeed));


    if(weldSystemInfo.scrapeUpArc ==1){
        ui->checkBox_8->setChecked(true);
    }else{
        ui->checkBox_8->setChecked(false);
    }

    ui->lineEdit_3->setText(QString("%1").arg(weldSystemInfo.scrapingDistance));
    ui->lineEdit_4->setText(QString("%1").arg(weldSystemInfo.returnStartPointSpeed));
    ui->lineEdit_5->setText(QString("%1").arg(weldSystemInfo.retryCount));

    if(weldSystemInfo.backArc ==1){
        ui->checkBox_9->setChecked(true);
    }else {
        ui->checkBox_9->setChecked(false);
    }
    ui->lineEdit_6->setText(QString("%1").arg(weldSystemInfo.numberOfArcs));

    ui->lineEdit_7->setText(QString("%1").arg(weldSystemInfo.defaultWeldspeed));

    if(weldSystemInfo.welding_microadjustment ==1){
        ui->checkBox_10->setChecked(true);
    }else {
        ui->checkBox_10->setChecked(false);
    }
    if(weldSystemInfo.useTPforwelding ==1){
        ui->checkBox_11->setChecked(true);
    }else {
        ui->checkBox_11->setChecked(false);
    }
    if(weldSystemInfo.remote_gas_cleaning ==1){
        ui->checkBox_12->setChecked(true);
    }else {
        ui->checkBox_12->setChecked(false);
    }
    if(weldSystemInfo.remote_inching_wirefeed ==1){
        ui->checkBox_13->setChecked(true);
    }else {
        ui->checkBox_13->setChecked(false);
    }
    if(weldSystemInfo.gas_cleaning_key ==1){
        ui->checkBox_14->setChecked(true);
    }else {
        ui->checkBox_14->setChecked(false);
    }
    ui->lineEdit_8->setText(QString("%1").arg(weldSystemInfo.gas_cleaning_time));

    ui->lineEdit_arcStartDetectTime->setText(QString("%1").arg(weldSystemInfo.arcStartDetectTime));
    ui->lineEdit_arcStartMaxWaitTime->setText(QString("%1").arg(weldSystemInfo.arcStartMaxWaitTime));
    ui->lineEdit_arcEndDelayAlarmTime->setText(QString("%1").arg(weldSystemInfo.arcEndDelayAlarmTime));
    ui->lineEdit_gasEndDelayAlarmTime->setText(QString("%1").arg(weldSystemInfo.gasEndDelayAlarmTime));
}

void WeldSystemDialog::on_returnBtn_clicked()
{
    this->hide();
}

void WeldSystemDialog::on_saveBtn_clicked()
{
    qDebug() << "保存";

    if(ui->checkBox->isChecked()){
        weldSystemInfo.arcDepletionDetection =1;
    }else{
        weldSystemInfo.arcDepletionDetection =0;
    }

    if(ui->checkBox_2->isChecked()){
        weldSystemInfo.gasDepletionDetection =1;
    }else {
        weldSystemInfo.gasDepletionDetection =0;
    }

    if(ui->checkBox_3->isChecked()){
        weldSystemInfo.weldwireDepletionDetection =1;
    }else {
        weldSystemInfo.weldwireDepletionDetection =0;
    }

    if(ui->checkBox_4->isChecked()){
        weldSystemInfo.solubleApplyTesting =1;
    }else {
        weldSystemInfo.solubleApplyTesting =0;
    }

    if(ui->checkBox_5->isChecked()){
        weldSystemInfo.powerFailureDetection =1;
    }else {
        weldSystemInfo.powerFailureDetection =0;
    }

    if(ui->checkBox_6->isChecked()){
        weldSystemInfo.weldwireDepletionDetection =1;
    }else {
        weldSystemInfo.weldwireDepletionDetection =0;
    }

    if(ui->checkBox_7->isChecked()){
        weldSystemInfo.toOpenAction =1;
    }else {
        weldSystemInfo.toOpenAction =0;
    }

    weldSystemInfo.toOpenDistance = ui->lineEdit->text().toDouble();
    weldSystemInfo.toOpenSpeed = ui->lineEdit_2->text().toDouble();

    if(ui->checkBox_8->isChecked()){
        weldSystemInfo.scrapeUpArc =1;
    }else {
        weldSystemInfo.scrapeUpArc =0;
    }

    weldSystemInfo.scrapingDistance = ui->lineEdit_3->text().toDouble();
    weldSystemInfo.returnStartPointSpeed = ui->lineEdit_4->text().toDouble();
    weldSystemInfo.retryCount = ui->lineEdit_5->text().toInt();

    if(ui->checkBox_9->isChecked()){
        weldSystemInfo.backArc =1;
    }else {
        weldSystemInfo.backArc =0;
    }

    weldSystemInfo.numberOfArcs = ui->lineEdit_6->text().toInt();
    weldSystemInfo.defaultWeldspeed = ui->lineEdit_7->text().toDouble();

    if(ui->checkBox_10->isChecked()){
        weldSystemInfo.welding_microadjustment =1;
    }else {
        weldSystemInfo.welding_microadjustment =0;
    }
    if(ui->checkBox_11->isChecked()){
        weldSystemInfo.useTPforwelding =1;
    }else {
        weldSystemInfo.useTPforwelding =0;
    }
    if(ui->checkBox_12->isChecked()){
        weldSystemInfo.remote_gas_cleaning =1;
    }else {
        weldSystemInfo.remote_gas_cleaning =0;
    }
    if(ui->checkBox_13->isChecked()){
        weldSystemInfo.remote_inching_wirefeed =1;
    }else {
        weldSystemInfo.remote_inching_wirefeed =0;
    }
    if(ui->checkBox_14->isChecked()){
        weldSystemInfo.gas_cleaning_key =1;
    }else {
        weldSystemInfo.gas_cleaning_key =0;
    }

    weldSystemInfo.gas_cleaning_time = ui->lineEdit_8->text().toDouble();


    QString str1 = ui->lineEdit->text();
    QString str2 = ui->lineEdit_2->text();
    QString str3 = ui->lineEdit_3->text();
    QString str4 = ui->lineEdit_4->text();
    QString str5 = ui->lineEdit_5->text();
    QString str6 = ui->lineEdit_6->text();
    QString str7 = ui->lineEdit_7->text();
    QString str8 = ui->lineEdit_8->text();

    weldSystemInfo.arcStartDetectTime=ui->lineEdit_arcStartDetectTime->text().toDouble();
    weldSystemInfo.arcStartMaxWaitTime=ui->lineEdit_arcStartMaxWaitTime->text().toDouble();
    weldSystemInfo.arcEndDelayAlarmTime=ui->lineEdit_arcEndDelayAlarmTime->text().toDouble();
    weldSystemInfo.gasEndDelayAlarmTime=ui->lineEdit_gasEndDelayAlarmTime->text().toDouble();

    QString pattern("^[0-9]+(.[0-9]{1,5})?$");
    QRegExp rxRoller(pattern);
    if(!rxRoller.exactMatch(str1)
            || !rxRoller.exactMatch(str2)
            || !rxRoller.exactMatch(str3)
            || !rxRoller.exactMatch(str4)
            || !rxRoller.exactMatch(str5)
            || !rxRoller.exactMatch(str6)
            || !rxRoller.exactMatch(str7)
            || !rxRoller.exactMatch(str8)){
       emit showErrorMsgBox(QString("存在非法字符，请修改！"));
       return;
    }else {
       //修改弧焊系统信息
       communicateNormalDelegate->changeWeldSystemInfo(weldSystemInfo);
       emit showErrorMsgBox(QString("保存成功！"));
       this->hide();
    }

}
